
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.temporal.ValueRange;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class SocketServer {
    public static int port =8080;
    public static void main(String [] agres){
        ServerSocket serverSocket = null;
        BufferedReader bufferedReader = null;
        PrintWriter writer = null;
        Socket socket = null;
        //设定服务端的端口号
        try {
            serverSocket=new ServerSocket(port);
            //等待请求,此方法会一直阻塞,直到获得请求才往下走
            socket=serverSocket.accept();
            //用于接收客户端发来的请求,得到输入流
            bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //用于发送返回信息,可以不需要装饰这么多io流使用缓冲流时发送数据要注意调用.flush()方法
            writer=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            int i=0;
            while(i<10) {if(i==0){
                String str=bufferedReader.readLine();//接收到的信息
                String[] split = str.split(":");
                if(split[1].equals("body"+i))
                {
                    writer.println("body"+(i+1));
                    writer.flush();
                    i++;
                }
            }
            else {
                String str=null;
                long l = System.currentTimeMillis();
                try {
                    try {
                        str = bufferedReader.readLine();//接收到的信息
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    long l1 = System.currentTimeMillis();
                    String[] split = str.split(":");
                    if(split.length<1){
                        System.out.println("丢失");
                    }
                    System.out.println(str);
                    if (  l1 - l < 1 && i != 9&&split.length>1) {
                        if(split[1].equals("body" + i)){
                            writer.println("body" + (i + 1));
                            writer.flush();
                            i++;}
                        else{
                            writer.println("body" + i + "错误重传");
                            writer.flush();
                        }
                    } else {
                        if (i != 9) {
                            writer.println("body" + i + "重传");
                            writer.flush();
                        } else {
                            System.out.println("结束");
                        }
                    }
                } finally {

                }
            }}

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                writer.close();
                socket.close();
                serverSocket.close();
            }catch (Exception e){

            }
        }
    }}
